package com;

import tool.jdbctool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "kaoshiServlet")
public class kaoshiServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        Connection connection=null;
        PreparedStatement statement=null;
        List<question>list=new ArrayList<>();
        ResultSet resultSet=null;
        try {
            connection= jdbctool.getconection();
            statement=connection.prepareStatement(
             "select *  from `question` ORDER BY RAND()limit 0,4");
            resultSet=statement.executeQuery();
            while (resultSet.next())
            {
                list.add(
                        new question(
                                resultSet.getInt(1),
                                resultSet.getString(2),
                                resultSet.getString(3),
                                resultSet.getString(4),
                                resultSet.getString(5),
                                resultSet.getString(6),
                                resultSet.getString(7)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        HttpSession session=request.getSession(false);
        session.setAttribute("keyl",list);
        request.getRequestDispatcher("/kaoshi.jsp")
                .forward(request,response);
    }
}
